<template>
  <el-dialog :title="showTitle" :visible="showDialog" @close="handleColse">
    <el-form ref="deptForm" label-width="120px" :model="deptForm" :rules="deptRules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="deptForm.name" size="mini" style="width:80%" placeholder="2-10个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="deptForm.code" size="mini" style="width:80%" placeholder="2-10个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="managerId">
        <el-select v-model="deptForm.managerId" size="mini" style="width:80%" placeholder="请选择负责人">
          <el-option
            v-for="item in managerList"
            :key="item.id"
            :label="item.username"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门名称" prop="introduce">
        <el-input v-model="deptForm.introduce" :rows="4" type="textarea" style="width:80%" placeholder="1-100个字符" />
      </el-form-item>
      <el-form-item>
        <el-row style="width:80%" type="flex" justify="center">
          <el-button size="mini" type="primary" @click="confirmClick">确定</el-button>
          <el-button size="mini" @click="handleColse">取消</el-button>
        </el-row>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { getDepartmentsAPI, getManagersAPI, addDeptmentAPI, getDepartmentDetailAPI, updateDepartmentAPI } from '@/api/department'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    parentId: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      managerList: [],
      deptForm: {
        name: '', // 部门名称
        code: '', // 部门编码
        managerId: '', // 部门负责人
        introduce: '', // 部门介绍
        pid: ''
      },
      deptRules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '长度为2-10个字符', trigger: 'blur' },
          {
            validator: async(rule, value, callback) => {
              // 获取部门列表
              let deptForm = await getDepartmentsAPI()
              // 区分编辑和新增模式
              if (this.deptForm.id) {
                deptForm = deptForm.filter(item => item.id !== this.deptForm.id)
              }
              // 利用数组的some方法，判断用户输入的名称是否已经存在
              if (deptForm.some(item => item.name === value)) {
                callback(new Error('部门名称已存在'))
              } else {
                callback()
              }
            }, trigger: 'blur'
          }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '长度为2-10个字符', trigger: 'blur' },
          {
            validator: async(rule, value, callback) => {
              // 获取部门列表
              let deptForm = await getDepartmentsAPI()
              // 区分编辑和新增模式
              if (this.deptForm.id) {
                deptForm = deptForm.filter(item => item.id !== this.deptForm.id)
              }
              // 利用数组的some方法，判断用户输入的编码是否已经存在
              if (deptForm.some(item => item.code === value)) {
                callback(new Error('部门编码已存在'))
              } else {
                callback()
              }
            }, trigger: 'blur'
          }

        ],
        managerId: [
          { required: true, message: '部门负责人不能为空', trigger: 'change' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 100, message: '长度为1-100个字符', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    showTitle() {
      return this.deptForm.id ? '编辑部门' : '添加子部门'
    }
  },
  created() {
    // 获取项目负责人列表
    this.getManagerList()
  },
  methods: {
    // 弹层组件自己根据部门id获取部门详情的数据并回显
    async getDepartmentDetail() {
      console.log(this.parentId)
      this.deptForm = await getDepartmentDetailAPI(this.parentId)
    },
    confirmClick() {
      // 对表单进行整体验证
      this.$refs.deptForm.validate(async(isOk) => {
        if (isOk) {
          if (this.deptForm.id) {
            // 编辑模式
            await updateDepartmentAPI(this.deptForm)
            // 提示用户编辑部门成功
            this.$message.success('修改部门成功')
          } else {
            // 校验成功：调用新增子部门的接口方法
            await addDeptmentAPI({ ...this.deptForm, pid: this.parentId })
            // 提示用户添加子部门成功
            this.$message.success('添加子部门成功')
          }
          // 通知父组件更新数据
          this.$emit('updateDept')
          // 重置表单+关闭弹层
          this.handleColse()
        }
      })
    },
    // 调用获取项目负责人接口方法
    async getManagerList() {
      const managerList = await getManagersAPI()
      this.managerList = managerList
    },
    handleColse() {
      // 还原表单数据
      this.deptForm = {
        name: '', // 部门名称
        code: '', // 部门编码
        managerId: '', // 部门负责人
        introduce: '', // 部门介绍
        pid: ''
      }
      this.$refs.deptForm.resetFields()
      // 关闭弹层
      this.$emit('update:showDialog', false)
    }
  }
}
</script>

<style>

</style>
